package com.ling.sdk.utils

import android.app.Activity
import android.content.Context
import android.widget.TextView
import android.widget.Toast
import com.ling.sdk.app.ActivityStack
import com.ling.sdk.app.App
import org.greenrobot.eventbus.EventBus

/**
 * Created by MinF on 2017/10/26 0026.
 */
object Toasty {

    //为每一个Act配备一个Toast
    private val toastPool = hashMapOf<String, Toast>()

    @JvmStatic
    fun showInfo(message: String?) {
        message?.showToast()
    }

    @JvmStatic
    fun showError(message: String?) {
        message?.showToast()
    }

    @JvmStatic
    fun showWarning(message: String?) {
        message?.showToast()
    }

    @JvmStatic
    fun showSuccess(message: String?) {
        message?.showToast()
    }

    private fun showText(message: String) {
        if (message.isEmpty()) return
        val act: Activity = ActivityStack.getTopActivity() ?: return
        act.runOnUiThread {
            val key = ActivityStack.genSafeKey(act)
            val toast = toastPool[key] ?: Toast.makeText(act,message,Toast.LENGTH_SHORT)
            toastPool[key] = toast
            toast.setMessage(message)
            toast.show()
        }
    }


    private fun Toast.setMessage(message: String) {
        (this.view as? TextView)?.text = message
    }

    private fun String.showToast() {
        showText(this)
    }

    fun activityDestroy(token: String) {
        toastPool.remove(token)
    }


}